
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.forms.widgets &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-forms-widgets">
            
  <h1>Source code for django.forms.widgets</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">HTML Widget classes</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">unicode_literals</span>

<span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">import</span> <span class="nn">datetime</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="k">import</span> <span class="n">chain</span>

<span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.forms.utils</span> <span class="k">import</span> <span class="n">to_current_timezone</span>
<span class="kn">from</span> <span class="nn">django.templatetags.static</span> <span class="k">import</span> <span class="n">static</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">datetime_safe</span><span class="p">,</span> <span class="n">formats</span><span class="p">,</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">django.utils.dates</span> <span class="k">import</span> <span class="n">MONTHS</span>
<span class="kn">from</span> <span class="nn">django.utils.deprecation</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">RemovedInDjango20Warning</span><span class="p">,</span> <span class="n">RenameMethodsBase</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">force_str</span><span class="p">,</span> <span class="n">force_text</span><span class="p">,</span> <span class="n">python_2_unicode_compatible</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.utils.formats</span> <span class="k">import</span> <span class="n">get_format</span>
<span class="kn">from</span> <span class="nn">django.utils.html</span> <span class="k">import</span> <span class="n">format_html</span><span class="p">,</span> <span class="n">html_safe</span>
<span class="kn">from</span> <span class="nn">django.utils.safestring</span> <span class="k">import</span> <span class="n">mark_safe</span>
<span class="kn">from</span> <span class="nn">django.utils.six.moves</span> <span class="k">import</span> <span class="nb">range</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">ugettext_lazy</span>

<span class="kn">from</span> <span class="nn">.renderers</span> <span class="k">import</span> <span class="n">get_default_renderer</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">(</span>
    <span class="s1">&#39;Media&#39;</span><span class="p">,</span> <span class="s1">&#39;MediaDefiningClass&#39;</span><span class="p">,</span> <span class="s1">&#39;Widget&#39;</span><span class="p">,</span> <span class="s1">&#39;TextInput&#39;</span><span class="p">,</span> <span class="s1">&#39;NumberInput&#39;</span><span class="p">,</span>
    <span class="s1">&#39;EmailInput&#39;</span><span class="p">,</span> <span class="s1">&#39;URLInput&#39;</span><span class="p">,</span> <span class="s1">&#39;PasswordInput&#39;</span><span class="p">,</span> <span class="s1">&#39;HiddenInput&#39;</span><span class="p">,</span>
    <span class="s1">&#39;MultipleHiddenInput&#39;</span><span class="p">,</span> <span class="s1">&#39;FileInput&#39;</span><span class="p">,</span> <span class="s1">&#39;ClearableFileInput&#39;</span><span class="p">,</span> <span class="s1">&#39;Textarea&#39;</span><span class="p">,</span>
    <span class="s1">&#39;DateInput&#39;</span><span class="p">,</span> <span class="s1">&#39;DateTimeInput&#39;</span><span class="p">,</span> <span class="s1">&#39;TimeInput&#39;</span><span class="p">,</span> <span class="s1">&#39;CheckboxInput&#39;</span><span class="p">,</span> <span class="s1">&#39;Select&#39;</span><span class="p">,</span>
    <span class="s1">&#39;NullBooleanSelect&#39;</span><span class="p">,</span> <span class="s1">&#39;SelectMultiple&#39;</span><span class="p">,</span> <span class="s1">&#39;RadioSelect&#39;</span><span class="p">,</span>
    <span class="s1">&#39;CheckboxSelectMultiple&#39;</span><span class="p">,</span> <span class="s1">&#39;MultiWidget&#39;</span><span class="p">,</span> <span class="s1">&#39;SplitDateTimeWidget&#39;</span><span class="p">,</span>
    <span class="s1">&#39;SplitHiddenDateTimeWidget&#39;</span><span class="p">,</span> <span class="s1">&#39;SelectDateWidget&#39;</span><span class="p">,</span>
<span class="p">)</span>

<span class="n">MEDIA_TYPES</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;css&#39;</span><span class="p">,</span> <span class="s1">&#39;js&#39;</span><span class="p">)</span>


<span class="nd">@html_safe</span>
<span class="nd">@python_2_unicode_compatible</span>
<span class="k">class</span> <span class="nc">Media</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">media</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">media</span><span class="p">:</span>
            <span class="n">media_attrs</span> <span class="o">=</span> <span class="n">media</span><span class="o">.</span><span class="vm">__dict__</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">media_attrs</span> <span class="o">=</span> <span class="n">kwargs</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_css</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_js</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">MEDIA_TYPES</span><span class="p">:</span>
            <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;add_&#39;</span> <span class="o">+</span> <span class="n">name</span><span class="p">)(</span><span class="n">media_attrs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">render</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">mark_safe</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">chain</span><span class="p">(</span><span class="o">*</span><span class="p">[</span><span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;render_&#39;</span> <span class="o">+</span> <span class="n">name</span><span class="p">)()</span> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">MEDIA_TYPES</span><span class="p">])))</span>

    <span class="k">def</span> <span class="nf">render_js</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[</span>
            <span class="n">format_html</span><span class="p">(</span>
                <span class="s1">&#39;&lt;script type=&quot;text/javascript&quot; src=&quot;</span><span class="si">{}</span><span class="s1">&quot;&gt;&lt;/script&gt;&#39;</span><span class="p">,</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">absolute_path</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
            <span class="p">)</span> <span class="k">for</span> <span class="n">path</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_js</span>
        <span class="p">]</span>

    <span class="k">def</span> <span class="nf">render_css</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># To keep rendering order consistent, we can&#39;t just iterate over items().</span>
        <span class="c1"># We need to sort the keys, and iterate over the sorted list.</span>
        <span class="n">media</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_css</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
        <span class="k">return</span> <span class="n">chain</span><span class="p">(</span><span class="o">*</span><span class="p">[[</span>
            <span class="n">format_html</span><span class="p">(</span>
                <span class="s1">&#39;&lt;link href=&quot;</span><span class="si">{}</span><span class="s1">&quot; type=&quot;text/css&quot; media=&quot;</span><span class="si">{}</span><span class="s1">&quot; rel=&quot;stylesheet&quot; /&gt;&#39;</span><span class="p">,</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">absolute_path</span><span class="p">(</span><span class="n">path</span><span class="p">),</span> <span class="n">medium</span>
            <span class="p">)</span> <span class="k">for</span> <span class="n">path</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_css</span><span class="p">[</span><span class="n">medium</span><span class="p">]</span>
        <span class="p">]</span> <span class="k">for</span> <span class="n">medium</span> <span class="ow">in</span> <span class="n">media</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">absolute_path</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Given a relative or absolute path to a static asset, return an absolute</span>
<span class="sd">        path. An absolute path will be returned unchanged while a relative path</span>
<span class="sd">        will be passed to django.templatetags.static.static().</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">startswith</span><span class="p">((</span><span class="s1">&#39;http://&#39;</span><span class="p">,</span> <span class="s1">&#39;https://&#39;</span><span class="p">,</span> <span class="s1">&#39;/&#39;</span><span class="p">)):</span>
            <span class="k">return</span> <span class="n">path</span>
        <span class="k">return</span> <span class="n">static</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="s2">&quot;Returns a Media object that only contains media of the given type&quot;</span>
        <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">MEDIA_TYPES</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">Media</span><span class="p">(</span><span class="o">**</span><span class="p">{</span><span class="nb">str</span><span class="p">(</span><span class="n">name</span><span class="p">):</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;_&#39;</span> <span class="o">+</span> <span class="n">name</span><span class="p">)})</span>
        <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span><span class="s1">&#39;Unknown media type &quot;</span><span class="si">%s</span><span class="s1">&quot;&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">add_js</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">data</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">path</span> <span class="ow">in</span> <span class="n">data</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">path</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_js</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_js</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">add_css</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">data</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">medium</span><span class="p">,</span> <span class="n">paths</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">for</span> <span class="n">path</span> <span class="ow">in</span> <span class="n">paths</span><span class="p">:</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_css</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">medium</span><span class="p">)</span> <span class="ow">or</span> <span class="n">path</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_css</span><span class="p">[</span><span class="n">medium</span><span class="p">]:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_css</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="n">medium</span><span class="p">,</span> <span class="p">[])</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__add__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="n">combined</span> <span class="o">=</span> <span class="n">Media</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">MEDIA_TYPES</span><span class="p">:</span>
            <span class="nb">getattr</span><span class="p">(</span><span class="n">combined</span><span class="p">,</span> <span class="s1">&#39;add_&#39;</span> <span class="o">+</span> <span class="n">name</span><span class="p">)(</span><span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;_&#39;</span> <span class="o">+</span> <span class="n">name</span><span class="p">,</span> <span class="kc">None</span><span class="p">))</span>
            <span class="nb">getattr</span><span class="p">(</span><span class="n">combined</span><span class="p">,</span> <span class="s1">&#39;add_&#39;</span> <span class="o">+</span> <span class="n">name</span><span class="p">)(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="s1">&#39;_&#39;</span> <span class="o">+</span> <span class="n">name</span><span class="p">,</span> <span class="kc">None</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">combined</span>


<span class="k">def</span> <span class="nf">media_property</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">_media</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># Get the media property of the superclass, if it exists</span>
        <span class="n">sup_cls</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">base</span> <span class="o">=</span> <span class="n">sup_cls</span><span class="o">.</span><span class="n">media</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="n">base</span> <span class="o">=</span> <span class="n">Media</span><span class="p">()</span>

        <span class="c1"># Get the media definition for this class</span>
        <span class="n">definition</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="s1">&#39;Media&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">definition</span><span class="p">:</span>
            <span class="n">extend</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">definition</span><span class="p">,</span> <span class="s1">&#39;extend&#39;</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">extend</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">extend</span> <span class="ow">is</span> <span class="kc">True</span><span class="p">:</span>
                    <span class="n">m</span> <span class="o">=</span> <span class="n">base</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">m</span> <span class="o">=</span> <span class="n">Media</span><span class="p">()</span>
                    <span class="k">for</span> <span class="n">medium</span> <span class="ow">in</span> <span class="n">extend</span><span class="p">:</span>
                        <span class="n">m</span> <span class="o">=</span> <span class="n">m</span> <span class="o">+</span> <span class="n">base</span><span class="p">[</span><span class="n">medium</span><span class="p">]</span>
                <span class="k">return</span> <span class="n">m</span> <span class="o">+</span> <span class="n">Media</span><span class="p">(</span><span class="n">definition</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">Media</span><span class="p">(</span><span class="n">definition</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">base</span>
    <span class="k">return</span> <span class="nb">property</span><span class="p">(</span><span class="n">_media</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">MediaDefiningClass</span><span class="p">(</span><span class="nb">type</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Metaclass for classes that can have media definitions.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__new__</span><span class="p">(</span><span class="n">mcs</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">bases</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span>
        <span class="n">new_class</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">MediaDefiningClass</span><span class="p">,</span> <span class="n">mcs</span><span class="p">)</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="n">mcs</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">bases</span><span class="p">,</span> <span class="n">attrs</span><span class="p">)</span>

        <span class="k">if</span> <span class="s1">&#39;media&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">attrs</span><span class="p">:</span>
            <span class="n">new_class</span><span class="o">.</span><span class="n">media</span> <span class="o">=</span> <span class="n">media_property</span><span class="p">(</span><span class="n">new_class</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">new_class</span>


<span class="k">class</span> <span class="nc">RenameWidgetMethods</span><span class="p">(</span><span class="n">MediaDefiningClass</span><span class="p">,</span> <span class="n">RenameMethodsBase</span><span class="p">):</span>
    <span class="n">renamed_methods</span> <span class="o">=</span> <span class="p">(</span>
        <span class="p">(</span><span class="s1">&#39;_format_value&#39;</span><span class="p">,</span> <span class="s1">&#39;format_value&#39;</span><span class="p">,</span> <span class="n">RemovedInDjango20Warning</span><span class="p">),</span>
    <span class="p">)</span>


<div class="viewcode-block" id="Widget"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.Widget">[docs]</a><span class="k">class</span> <span class="nc">Widget</span><span class="p">(</span><span class="n">six</span><span class="o">.</span><span class="n">with_metaclass</span><span class="p">(</span><span class="n">RenameWidgetMethods</span><span class="p">)):</span>
    <span class="n">needs_multipart_form</span> <span class="o">=</span> <span class="kc">False</span>  <span class="c1"># Determines does this widget need multipart form</span>
    <span class="n">is_localized</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">is_required</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">supports_microseconds</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">attrs</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">attrs</span> <span class="o">=</span> <span class="n">attrs</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">attrs</span> <span class="o">=</span> <span class="p">{}</span>

    <span class="k">def</span> <span class="nf">__deepcopy__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">memo</span><span class="p">):</span>
        <span class="n">obj</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">attrs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">attrs</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="n">memo</span><span class="p">[</span><span class="nb">id</span><span class="p">(</span><span class="bp">self</span><span class="p">)]</span> <span class="o">=</span> <span class="n">obj</span>
        <span class="k">return</span> <span class="n">obj</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">is_hidden</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">input_type</span> <span class="o">==</span> <span class="s1">&#39;hidden&#39;</span> <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;input_type&#39;</span><span class="p">)</span> <span class="k">else</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">subwidgets</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">context</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">)</span>
        <span class="k">yield</span> <span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">]</span>

<div class="viewcode-block" id="Widget.format_value"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.Widget.format_value">[docs]</a>    <span class="k">def</span> <span class="nf">format_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a value as it should appear when rendered in a template.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">value</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span> <span class="ow">or</span> <span class="n">value</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_localized</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">formats</span><span class="o">.</span><span class="n">localize_input</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">force_text</span><span class="p">(</span><span class="n">value</span><span class="p">)</span></div>

<div class="viewcode-block" id="Widget.get_context"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.Widget.get_context">[docs]</a>    <span class="k">def</span> <span class="nf">get_context</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span>
        <span class="n">context</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">name</span><span class="p">,</span>
            <span class="s1">&#39;is_hidden&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_hidden</span><span class="p">,</span>
            <span class="s1">&#39;required&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_required</span><span class="p">,</span>
            <span class="s1">&#39;value&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">format_value</span><span class="p">(</span><span class="n">value</span><span class="p">),</span>
            <span class="s1">&#39;attrs&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">build_attrs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">attrs</span><span class="p">,</span> <span class="n">attrs</span><span class="p">),</span>
            <span class="s1">&#39;template_name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">template_name</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="n">context</span></div>

<div class="viewcode-block" id="Widget.render"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.Widget.render">[docs]</a>    <span class="k">def</span> <span class="nf">render</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">renderer</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns this Widget rendered as HTML, as a Unicode string.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">context</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_render</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">template_name</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span> <span class="n">renderer</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_render</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">template_name</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span> <span class="n">renderer</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">renderer</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">renderer</span> <span class="o">=</span> <span class="n">get_default_renderer</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">mark_safe</span><span class="p">(</span><span class="n">renderer</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">template_name</span><span class="p">,</span> <span class="n">context</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">build_attrs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">base_attrs</span><span class="p">,</span> <span class="n">extra_attrs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="s2">&quot;Helper function for building an attribute dictionary.&quot;</span>
        <span class="n">attrs</span> <span class="o">=</span> <span class="n">base_attrs</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">extra_attrs</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">attrs</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">extra_attrs</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">attrs</span>

<div class="viewcode-block" id="Widget.value_from_datadict"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.Widget.value_from_datadict">[docs]</a>    <span class="k">def</span> <span class="nf">value_from_datadict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Given a dictionary of data and this widget&#39;s name, returns the value</span>
<span class="sd">        of this widget. Returns None if it&#39;s not provided.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">)</span></div>

<div class="viewcode-block" id="Widget.value_omitted_from_data"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.Widget.value_omitted_from_data">[docs]</a>    <span class="k">def</span> <span class="nf">value_omitted_from_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">data</span></div>

<div class="viewcode-block" id="Widget.id_for_label"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.Widget.id_for_label">[docs]</a>    <span class="k">def</span> <span class="nf">id_for_label</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">id_</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the HTML ID attribute of this Widget for use by a &lt;label&gt;,</span>
<span class="sd">        given the ID of the field. Returns None if no ID is available.</span>

<span class="sd">        This hook is necessary because some widgets have multiple HTML</span>
<span class="sd">        elements and, thus, multiple IDs. In that case, this method should</span>
<span class="sd">        return an ID value that corresponds to the first ID in the widget&#39;s</span>
<span class="sd">        tags.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">id_</span></div>

<div class="viewcode-block" id="Widget.use_required_attribute"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.Widget.use_required_attribute">[docs]</a>    <span class="k">def</span> <span class="nf">use_required_attribute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">initial</span><span class="p">):</span>
        <span class="k">return</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_hidden</span></div></div>


<span class="k">class</span> <span class="nc">Input</span><span class="p">(</span><span class="n">Widget</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Base class for all &lt;input&gt; widgets.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">input_type</span> <span class="o">=</span> <span class="kc">None</span>  <span class="c1"># Subclasses must define this.</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/input.html&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">attrs</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">attrs</span> <span class="o">=</span> <span class="n">attrs</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">input_type</span> <span class="o">=</span> <span class="n">attrs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;type&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">input_type</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Input</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">attrs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_context</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span>
        <span class="n">context</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">Input</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">)</span>
        <span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">][</span><span class="s1">&#39;type&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">input_type</span>
        <span class="k">return</span> <span class="n">context</span>


<div class="viewcode-block" id="TextInput"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.TextInput">[docs]</a><span class="k">class</span> <span class="nc">TextInput</span><span class="p">(</span><span class="n">Input</span><span class="p">):</span>
    <span class="n">input_type</span> <span class="o">=</span> <span class="s1">&#39;text&#39;</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/text.html&#39;</span></div>


<div class="viewcode-block" id="NumberInput"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.NumberInput">[docs]</a><span class="k">class</span> <span class="nc">NumberInput</span><span class="p">(</span><span class="n">Input</span><span class="p">):</span>
    <span class="n">input_type</span> <span class="o">=</span> <span class="s1">&#39;number&#39;</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/number.html&#39;</span></div>


<div class="viewcode-block" id="EmailInput"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.EmailInput">[docs]</a><span class="k">class</span> <span class="nc">EmailInput</span><span class="p">(</span><span class="n">Input</span><span class="p">):</span>
    <span class="n">input_type</span> <span class="o">=</span> <span class="s1">&#39;email&#39;</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/email.html&#39;</span></div>


<div class="viewcode-block" id="URLInput"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.URLInput">[docs]</a><span class="k">class</span> <span class="nc">URLInput</span><span class="p">(</span><span class="n">Input</span><span class="p">):</span>
    <span class="n">input_type</span> <span class="o">=</span> <span class="s1">&#39;url&#39;</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/url.html&#39;</span></div>


<div class="viewcode-block" id="PasswordInput"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.PasswordInput">[docs]</a><span class="k">class</span> <span class="nc">PasswordInput</span><span class="p">(</span><span class="n">Input</span><span class="p">):</span>
    <span class="n">input_type</span> <span class="o">=</span> <span class="s1">&#39;password&#39;</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/password.html&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">render_value</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">PasswordInput</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">attrs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">render_value</span> <span class="o">=</span> <span class="n">render_value</span>

    <span class="k">def</span> <span class="nf">get_context</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_value</span><span class="p">:</span>
            <span class="n">value</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">PasswordInput</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">)</span></div>


<div class="viewcode-block" id="HiddenInput"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.HiddenInput">[docs]</a><span class="k">class</span> <span class="nc">HiddenInput</span><span class="p">(</span><span class="n">Input</span><span class="p">):</span>
    <span class="n">input_type</span> <span class="o">=</span> <span class="s1">&#39;hidden&#39;</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/hidden.html&#39;</span></div>


<div class="viewcode-block" id="MultipleHiddenInput"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.MultipleHiddenInput">[docs]</a><span class="k">class</span> <span class="nc">MultipleHiddenInput</span><span class="p">(</span><span class="n">HiddenInput</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A widget that handles &lt;input type=&quot;hidden&quot;&gt; for fields that have a list</span>
<span class="sd">    of values.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/multiple_hidden.html&#39;</span>

    <span class="k">def</span> <span class="nf">get_context</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span>
        <span class="n">context</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">MultipleHiddenInput</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">)</span>
        <span class="n">final_attrs</span> <span class="o">=</span> <span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">][</span><span class="s1">&#39;attrs&#39;</span><span class="p">]</span>
        <span class="n">id_</span> <span class="o">=</span> <span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">][</span><span class="s1">&#39;attrs&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">)</span>

        <span class="n">subwidgets</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="n">value_</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">][</span><span class="s1">&#39;value&#39;</span><span class="p">]):</span>
            <span class="n">widget_attrs</span> <span class="o">=</span> <span class="n">final_attrs</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">id_</span><span class="p">:</span>
                <span class="c1"># An ID attribute was given. Add a numeric index as a suffix</span>
                <span class="c1"># so that the inputs don&#39;t all have the same ID attribute.</span>
                <span class="n">widget_attrs</span><span class="p">[</span><span class="s1">&#39;id&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">id_</span><span class="p">,</span> <span class="n">index</span><span class="p">)</span>
            <span class="n">widget</span> <span class="o">=</span> <span class="n">HiddenInput</span><span class="p">()</span>
            <span class="n">widget</span><span class="o">.</span><span class="n">is_required</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_required</span>
            <span class="n">subwidgets</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">widget</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value_</span><span class="p">,</span> <span class="n">widget_attrs</span><span class="p">)[</span><span class="s1">&#39;widget&#39;</span><span class="p">])</span>

        <span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">][</span><span class="s1">&#39;subwidgets&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">subwidgets</span>
        <span class="k">return</span> <span class="n">context</span>

    <span class="k">def</span> <span class="nf">value_from_datadict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">getter</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">getlist</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="n">getter</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">get</span>
        <span class="k">return</span> <span class="n">getter</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">format_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[]</span> <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">value</span></div>


<div class="viewcode-block" id="FileInput"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.FileInput">[docs]</a><span class="k">class</span> <span class="nc">FileInput</span><span class="p">(</span><span class="n">Input</span><span class="p">):</span>
    <span class="n">input_type</span> <span class="o">=</span> <span class="s1">&#39;file&#39;</span>
    <span class="n">needs_multipart_form</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/file.html&#39;</span>

    <span class="k">def</span> <span class="nf">format_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;File input never renders a value.&quot;&quot;&quot;</span>
        <span class="k">return</span>

    <span class="k">def</span> <span class="nf">value_from_datadict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="s2">&quot;File widgets take data from FILES, not POST&quot;</span>
        <span class="k">return</span> <span class="n">files</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">value_omitted_from_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">files</span></div>


<span class="n">FILE_INPUT_CONTRADICTION</span> <span class="o">=</span> <span class="nb">object</span><span class="p">()</span>


<div class="viewcode-block" id="ClearableFileInput"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.ClearableFileInput">[docs]</a><span class="k">class</span> <span class="nc">ClearableFileInput</span><span class="p">(</span><span class="n">FileInput</span><span class="p">):</span>
    <span class="n">clear_checkbox_label</span> <span class="o">=</span> <span class="n">ugettext_lazy</span><span class="p">(</span><span class="s1">&#39;Clear&#39;</span><span class="p">)</span>
    <span class="n">initial_text</span> <span class="o">=</span> <span class="n">ugettext_lazy</span><span class="p">(</span><span class="s1">&#39;Currently&#39;</span><span class="p">)</span>
    <span class="n">input_text</span> <span class="o">=</span> <span class="n">ugettext_lazy</span><span class="p">(</span><span class="s1">&#39;Change&#39;</span><span class="p">)</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/clearable_file_input.html&#39;</span>

    <span class="k">def</span> <span class="nf">clear_checkbox_name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Given the name of the file input, return the name of the clear checkbox</span>
<span class="sd">        input.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">name</span> <span class="o">+</span> <span class="s1">&#39;-clear&#39;</span>

    <span class="k">def</span> <span class="nf">clear_checkbox_id</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Given the name of the clear checkbox input, return the HTML id for it.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">name</span> <span class="o">+</span> <span class="s1">&#39;_id&#39;</span>

    <span class="k">def</span> <span class="nf">is_initial</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return whether value is considered to be initial value.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">bool</span><span class="p">(</span><span class="n">value</span> <span class="ow">and</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="s1">&#39;url&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">format_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the file object if it has a defined url attribute.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_initial</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">value</span>

    <span class="k">def</span> <span class="nf">get_context</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span>
        <span class="n">context</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ClearableFileInput</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">)</span>
        <span class="n">checkbox_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">clear_checkbox_name</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
        <span class="n">checkbox_id</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">clear_checkbox_id</span><span class="p">(</span><span class="n">checkbox_name</span><span class="p">)</span>
        <span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">update</span><span class="p">({</span>
            <span class="s1">&#39;checkbox_name&#39;</span><span class="p">:</span> <span class="n">checkbox_name</span><span class="p">,</span>
            <span class="s1">&#39;checkbox_id&#39;</span><span class="p">:</span> <span class="n">checkbox_id</span><span class="p">,</span>
            <span class="s1">&#39;is_initial&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_initial</span><span class="p">(</span><span class="n">value</span><span class="p">),</span>
            <span class="s1">&#39;input_text&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">input_text</span><span class="p">,</span>
            <span class="s1">&#39;initial_text&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_text</span><span class="p">,</span>
            <span class="s1">&#39;clear_checkbox_label&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">clear_checkbox_label</span><span class="p">,</span>
        <span class="p">})</span>
        <span class="k">return</span> <span class="n">context</span>

    <span class="k">def</span> <span class="nf">value_from_datadict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="n">upload</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ClearableFileInput</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">value_from_datadict</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_required</span> <span class="ow">and</span> <span class="n">CheckboxInput</span><span class="p">()</span><span class="o">.</span><span class="n">value_from_datadict</span><span class="p">(</span>
                <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">clear_checkbox_name</span><span class="p">(</span><span class="n">name</span><span class="p">)):</span>

            <span class="k">if</span> <span class="n">upload</span><span class="p">:</span>
                <span class="c1"># If the user contradicts themselves (uploads a new file AND</span>
                <span class="c1"># checks the &quot;clear&quot; checkbox), we return a unique marker</span>
                <span class="c1"># object that FileField will turn into a ValidationError.</span>
                <span class="k">return</span> <span class="n">FILE_INPUT_CONTRADICTION</span>
            <span class="c1"># False signals to clear any existing value, as opposed to just None</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="n">upload</span>

    <span class="k">def</span> <span class="nf">use_required_attribute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">initial</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">ClearableFileInput</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">use_required_attribute</span><span class="p">(</span><span class="n">initial</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">initial</span>

    <span class="k">def</span> <span class="nf">value_omitted_from_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="nb">super</span><span class="p">(</span><span class="n">ClearableFileInput</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">value_omitted_from_data</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span> <span class="ow">and</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">clear_checkbox_name</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">data</span>
        <span class="p">)</span></div>


<div class="viewcode-block" id="Textarea"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.Textarea">[docs]</a><span class="k">class</span> <span class="nc">Textarea</span><span class="p">(</span><span class="n">Widget</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/textarea.html&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># Use slightly better defaults than HTML&#39;s 20x2 box</span>
        <span class="n">default_attrs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;cols&#39;</span><span class="p">:</span> <span class="s1">&#39;40&#39;</span><span class="p">,</span> <span class="s1">&#39;rows&#39;</span><span class="p">:</span> <span class="s1">&#39;10&#39;</span><span class="p">}</span>
        <span class="k">if</span> <span class="n">attrs</span><span class="p">:</span>
            <span class="n">default_attrs</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">attrs</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Textarea</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">default_attrs</span><span class="p">)</span></div>


<span class="k">class</span> <span class="nc">DateTimeBaseInput</span><span class="p">(</span><span class="n">TextInput</span><span class="p">):</span>
    <span class="n">format_key</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
    <span class="n">supports_microseconds</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">DateTimeBaseInput</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">attrs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">format</span> <span class="o">=</span> <span class="nb">format</span> <span class="k">if</span> <span class="nb">format</span> <span class="k">else</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">format_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># localize_input() returns str on Python 2.</span>
            <span class="k">return</span> <span class="n">force_text</span><span class="p">(</span><span class="n">formats</span><span class="o">.</span><span class="n">localize_input</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">format</span> <span class="ow">or</span> <span class="n">formats</span><span class="o">.</span><span class="n">get_format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">format_key</span><span class="p">)[</span><span class="mi">0</span><span class="p">]))</span>


<div class="viewcode-block" id="DateInput"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.DateInput">[docs]</a><span class="k">class</span> <span class="nc">DateInput</span><span class="p">(</span><span class="n">DateTimeBaseInput</span><span class="p">):</span>
    <span class="n">format_key</span> <span class="o">=</span> <span class="s1">&#39;DATE_INPUT_FORMATS&#39;</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/date.html&#39;</span></div>


<div class="viewcode-block" id="DateTimeInput"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.DateTimeInput">[docs]</a><span class="k">class</span> <span class="nc">DateTimeInput</span><span class="p">(</span><span class="n">DateTimeBaseInput</span><span class="p">):</span>
    <span class="n">format_key</span> <span class="o">=</span> <span class="s1">&#39;DATETIME_INPUT_FORMATS&#39;</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/datetime.html&#39;</span></div>


<div class="viewcode-block" id="TimeInput"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.TimeInput">[docs]</a><span class="k">class</span> <span class="nc">TimeInput</span><span class="p">(</span><span class="n">DateTimeBaseInput</span><span class="p">):</span>
    <span class="n">format_key</span> <span class="o">=</span> <span class="s1">&#39;TIME_INPUT_FORMATS&#39;</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/time.html&#39;</span></div>


<span class="c1"># Defined at module level so that CheckboxInput is picklable (#17976)</span>
<span class="k">def</span> <span class="nf">boolean_check</span><span class="p">(</span><span class="n">v</span><span class="p">):</span>
    <span class="k">return</span> <span class="ow">not</span> <span class="p">(</span><span class="n">v</span> <span class="ow">is</span> <span class="kc">False</span> <span class="ow">or</span> <span class="n">v</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">v</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>


<div class="viewcode-block" id="CheckboxInput"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.CheckboxInput">[docs]</a><span class="k">class</span> <span class="nc">CheckboxInput</span><span class="p">(</span><span class="n">Input</span><span class="p">):</span>
    <span class="n">input_type</span> <span class="o">=</span> <span class="s1">&#39;checkbox&#39;</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/checkbox.html&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">check_test</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">CheckboxInput</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">attrs</span><span class="p">)</span>
        <span class="c1"># check_test is a callable that takes a value and returns True</span>
        <span class="c1"># if the checkbox should be checked for that value.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_test</span> <span class="o">=</span> <span class="n">boolean_check</span> <span class="k">if</span> <span class="n">check_test</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">check_test</span>

    <span class="k">def</span> <span class="nf">format_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Only return the &#39;value&#39; attribute if value isn&#39;t empty.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="kc">True</span> <span class="ow">or</span> <span class="n">value</span> <span class="ow">is</span> <span class="kc">False</span> <span class="ow">or</span> <span class="n">value</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">value</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="k">return</span> <span class="n">force_text</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_context</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">check_test</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">attrs</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">attrs</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="n">attrs</span><span class="p">[</span><span class="s1">&#39;checked&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">CheckboxInput</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">value_from_datadict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">data</span><span class="p">:</span>
            <span class="c1"># A missing value means False because HTML form submission does not</span>
            <span class="c1"># send results for unselected checkboxes.</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="n">value</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
        <span class="c1"># Translate true and false strings to boolean values.</span>
        <span class="n">values</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;true&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span> <span class="s1">&#39;false&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">}</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">values</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">value</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span> <span class="n">value</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">bool</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">value_omitted_from_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="c1"># HTML checkboxes don&#39;t appear in POST data if not checked, so it&#39;s</span>
        <span class="c1"># never known if the value is actually omitted.</span>
        <span class="k">return</span> <span class="kc">False</span></div>


<span class="k">class</span> <span class="nc">ChoiceWidget</span><span class="p">(</span><span class="n">Widget</span><span class="p">):</span>
    <span class="n">allow_multiple_selected</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">input_type</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">option_template_name</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">add_id_index</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">checked_attribute</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;checked&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">}</span>
    <span class="n">option_inherits_attrs</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="p">()):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">ChoiceWidget</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">attrs</span><span class="p">)</span>
        <span class="c1"># choices can be any iterable, but we may need to render this widget</span>
        <span class="c1"># multiple times. Thus, collapse it into a list so it can be consumed</span>
        <span class="c1"># more than once.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">choices</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">choices</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__deepcopy__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">memo</span><span class="p">):</span>
        <span class="n">obj</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">attrs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">attrs</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">choices</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">choices</span><span class="p">)</span>
        <span class="n">memo</span><span class="p">[</span><span class="nb">id</span><span class="p">(</span><span class="bp">self</span><span class="p">)]</span> <span class="o">=</span> <span class="n">obj</span>
        <span class="k">return</span> <span class="n">obj</span>

    <span class="k">def</span> <span class="nf">subwidgets</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Yield all &quot;subwidgets&quot; of this widget. Used to enable iterating</span>
<span class="sd">        options from a BoundField for choice widgets.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">format_value</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">option</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span>
            <span class="k">yield</span> <span class="n">option</span>

    <span class="k">def</span> <span class="nf">options</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Yield a flat list of options for this widgets.&quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">group</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">optgroups</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">option</span> <span class="ow">in</span> <span class="n">group</span><span class="p">[</span><span class="mi">1</span><span class="p">]:</span>
                <span class="k">yield</span> <span class="n">option</span>

    <span class="k">def</span> <span class="nf">optgroups</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a list of optgroups for this widget.&quot;&quot;&quot;</span>
        <span class="n">groups</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">has_selected</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="p">(</span><span class="n">option_value</span><span class="p">,</span> <span class="n">option_label</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">chain</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">choices</span><span class="p">)):</span>
            <span class="k">if</span> <span class="n">option_value</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">option_value</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>

            <span class="n">subgroup</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">option_label</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
                <span class="n">group_name</span> <span class="o">=</span> <span class="n">option_value</span>
                <span class="n">subindex</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="n">choices</span> <span class="o">=</span> <span class="n">option_label</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">group_name</span> <span class="o">=</span> <span class="kc">None</span>
                <span class="n">subindex</span> <span class="o">=</span> <span class="kc">None</span>
                <span class="n">choices</span> <span class="o">=</span> <span class="p">[(</span><span class="n">option_value</span><span class="p">,</span> <span class="n">option_label</span><span class="p">)]</span>
            <span class="n">groups</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">group_name</span><span class="p">,</span> <span class="n">subgroup</span><span class="p">,</span> <span class="n">index</span><span class="p">))</span>

            <span class="k">for</span> <span class="n">subvalue</span><span class="p">,</span> <span class="n">sublabel</span> <span class="ow">in</span> <span class="n">choices</span><span class="p">:</span>
                <span class="n">selected</span> <span class="o">=</span> <span class="p">(</span>
                    <span class="n">force_text</span><span class="p">(</span><span class="n">subvalue</span><span class="p">)</span> <span class="ow">in</span> <span class="n">value</span> <span class="ow">and</span>
                    <span class="p">(</span><span class="n">has_selected</span> <span class="ow">is</span> <span class="kc">False</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_multiple_selected</span><span class="p">)</span>
                <span class="p">)</span>
                <span class="k">if</span> <span class="n">selected</span> <span class="ow">is</span> <span class="kc">True</span> <span class="ow">and</span> <span class="n">has_selected</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
                    <span class="n">has_selected</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="n">subgroup</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">create_option</span><span class="p">(</span>
                    <span class="n">name</span><span class="p">,</span> <span class="n">subvalue</span><span class="p">,</span> <span class="n">sublabel</span><span class="p">,</span> <span class="n">selected</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span>
                    <span class="n">subindex</span><span class="o">=</span><span class="n">subindex</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="n">attrs</span><span class="p">,</span>
                <span class="p">))</span>
                <span class="k">if</span> <span class="n">subindex</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">subindex</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="n">groups</span>

    <span class="k">def</span> <span class="nf">create_option</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">label</span><span class="p">,</span> <span class="n">selected</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">subindex</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">index</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">index</span><span class="p">)</span> <span class="k">if</span> <span class="n">subindex</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">_</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">index</span><span class="p">,</span> <span class="n">subindex</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">attrs</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">attrs</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">option_attrs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">build_attrs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">attrs</span><span class="p">,</span> <span class="n">attrs</span><span class="p">)</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">option_inherits_attrs</span> <span class="k">else</span> <span class="p">{}</span>
        <span class="k">if</span> <span class="n">selected</span><span class="p">:</span>
            <span class="n">option_attrs</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">checked_attribute</span><span class="p">)</span>
        <span class="k">if</span> <span class="s1">&#39;id&#39;</span> <span class="ow">in</span> <span class="n">option_attrs</span><span class="p">:</span>
            <span class="n">option_attrs</span><span class="p">[</span><span class="s1">&#39;id&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">id_for_label</span><span class="p">(</span><span class="n">option_attrs</span><span class="p">[</span><span class="s1">&#39;id&#39;</span><span class="p">],</span> <span class="n">index</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="n">name</span><span class="p">,</span>
            <span class="s1">&#39;value&#39;</span><span class="p">:</span> <span class="n">value</span><span class="p">,</span>
            <span class="s1">&#39;label&#39;</span><span class="p">:</span> <span class="n">label</span><span class="p">,</span>
            <span class="s1">&#39;selected&#39;</span><span class="p">:</span> <span class="n">selected</span><span class="p">,</span>
            <span class="s1">&#39;index&#39;</span><span class="p">:</span> <span class="n">index</span><span class="p">,</span>
            <span class="s1">&#39;attrs&#39;</span><span class="p">:</span> <span class="n">option_attrs</span><span class="p">,</span>
            <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">input_type</span><span class="p">,</span>
            <span class="s1">&#39;template_name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">option_template_name</span><span class="p">,</span>
        <span class="p">}</span>

    <span class="k">def</span> <span class="nf">get_context</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span>
        <span class="n">context</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ChoiceWidget</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">)</span>
        <span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">][</span><span class="s1">&#39;optgroups&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">optgroups</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">][</span><span class="s1">&#39;value&#39;</span><span class="p">],</span> <span class="n">attrs</span><span class="p">)</span>
        <span class="n">context</span><span class="p">[</span><span class="s1">&#39;wrap_label&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="n">context</span>

    <span class="k">def</span> <span class="nf">id_for_label</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">id_</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="s1">&#39;0&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Use an incremented id for each option where the main widget</span>
<span class="sd">        references the zero index.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">id_</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">add_id_index</span><span class="p">:</span>
            <span class="n">id_</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">id_</span><span class="p">,</span> <span class="n">index</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">id_</span>

    <span class="k">def</span> <span class="nf">value_from_datadict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="n">getter</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">get</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_multiple_selected</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">getter</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">getlist</span>
            <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                <span class="k">pass</span>
        <span class="k">return</span> <span class="n">getter</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">format_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return selected values as a list.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_multiple_selected</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">list</span><span class="p">)):</span>
            <span class="n">value</span> <span class="o">=</span> <span class="p">[</span><span class="n">value</span><span class="p">]</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">force_text</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="k">if</span> <span class="n">v</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">value</span><span class="p">]</span>


<div class="viewcode-block" id="Select"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.Select">[docs]</a><span class="k">class</span> <span class="nc">Select</span><span class="p">(</span><span class="n">ChoiceWidget</span><span class="p">):</span>
    <span class="n">input_type</span> <span class="o">=</span> <span class="s1">&#39;select&#39;</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/select.html&#39;</span>
    <span class="n">option_template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/select_option.html&#39;</span>
    <span class="n">add_id_index</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">checked_attribute</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;selected&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">}</span>
    <span class="n">option_inherits_attrs</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">get_context</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span>
        <span class="n">context</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">Select</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_multiple_selected</span><span class="p">:</span>
            <span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">][</span><span class="s1">&#39;attrs&#39;</span><span class="p">][</span><span class="s1">&#39;multiple&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;multiple&#39;</span>
        <span class="k">return</span> <span class="n">context</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_choice_has_empty_value</span><span class="p">(</span><span class="n">choice</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return True if the choice&#39;s value is empty string or None.&quot;&quot;&quot;</span>
        <span class="n">value</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">choice</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">bool</span><span class="p">(</span><span class="n">value</span><span class="p">))</span> <span class="ow">or</span>
            <span class="n">value</span> <span class="ow">is</span> <span class="kc">None</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">use_required_attribute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">initial</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Don&#39;t render &#39;required&#39; if the first &lt;option&gt; has a value, as that&#39;s</span>
<span class="sd">        invalid HTML.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">use_required_attribute</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">Select</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">use_required_attribute</span><span class="p">(</span><span class="n">initial</span><span class="p">)</span>
        <span class="c1"># &#39;required&#39; is always okay for &lt;select multiple&gt;.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_multiple_selected</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">use_required_attribute</span>

        <span class="n">first_choice</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">choices</span><span class="p">),</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">use_required_attribute</span> <span class="ow">and</span> <span class="n">first_choice</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_choice_has_empty_value</span><span class="p">(</span><span class="n">first_choice</span><span class="p">)</span></div>


<div class="viewcode-block" id="NullBooleanSelect"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.NullBooleanSelect">[docs]</a><span class="k">class</span> <span class="nc">NullBooleanSelect</span><span class="p">(</span><span class="n">Select</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A Select Widget intended to be used with NullBooleanField.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">choices</span> <span class="o">=</span> <span class="p">(</span>
            <span class="p">(</span><span class="s1">&#39;1&#39;</span><span class="p">,</span> <span class="n">ugettext_lazy</span><span class="p">(</span><span class="s1">&#39;Unknown&#39;</span><span class="p">)),</span>
            <span class="p">(</span><span class="s1">&#39;2&#39;</span><span class="p">,</span> <span class="n">ugettext_lazy</span><span class="p">(</span><span class="s1">&#39;Yes&#39;</span><span class="p">)),</span>
            <span class="p">(</span><span class="s1">&#39;3&#39;</span><span class="p">,</span> <span class="n">ugettext_lazy</span><span class="p">(</span><span class="s1">&#39;No&#39;</span><span class="p">)),</span>
        <span class="p">)</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">NullBooleanSelect</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">attrs</span><span class="p">,</span> <span class="n">choices</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">format_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">{</span><span class="kc">True</span><span class="p">:</span> <span class="s1">&#39;2&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">:</span> <span class="s1">&#39;3&#39;</span><span class="p">,</span> <span class="s1">&#39;2&#39;</span><span class="p">:</span> <span class="s1">&#39;2&#39;</span><span class="p">,</span> <span class="s1">&#39;3&#39;</span><span class="p">:</span> <span class="s1">&#39;3&#39;</span><span class="p">}[</span><span class="n">value</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="k">return</span> <span class="s1">&#39;1&#39;</span>

    <span class="k">def</span> <span class="nf">value_from_datadict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="n">value</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s1">&#39;2&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
            <span class="kc">True</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
            <span class="s1">&#39;True&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
            <span class="s1">&#39;3&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
            <span class="s1">&#39;False&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
            <span class="kc">False</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
        <span class="p">}</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">value</span><span class="p">)</span></div>


<div class="viewcode-block" id="SelectMultiple"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.SelectMultiple">[docs]</a><span class="k">class</span> <span class="nc">SelectMultiple</span><span class="p">(</span><span class="n">Select</span><span class="p">):</span>
    <span class="n">allow_multiple_selected</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">value_from_datadict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">getter</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">getlist</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="n">getter</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">get</span>
        <span class="k">return</span> <span class="n">getter</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">value_omitted_from_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="c1"># An unselected &lt;select multiple&gt; doesn&#39;t appear in POST data, so it&#39;s</span>
        <span class="c1"># never known if the value is actually omitted.</span>
        <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="RadioSelect"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.RadioSelect">[docs]</a><span class="k">class</span> <span class="nc">RadioSelect</span><span class="p">(</span><span class="n">ChoiceWidget</span><span class="p">):</span>
    <span class="n">input_type</span> <span class="o">=</span> <span class="s1">&#39;radio&#39;</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/radio.html&#39;</span>
    <span class="n">option_template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/radio_option.html&#39;</span></div>


<div class="viewcode-block" id="CheckboxSelectMultiple"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.CheckboxSelectMultiple">[docs]</a><span class="k">class</span> <span class="nc">CheckboxSelectMultiple</span><span class="p">(</span><span class="n">ChoiceWidget</span><span class="p">):</span>
    <span class="n">allow_multiple_selected</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">input_type</span> <span class="o">=</span> <span class="s1">&#39;checkbox&#39;</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/checkbox_select.html&#39;</span>
    <span class="n">option_template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/checkbox_option.html&#39;</span>

    <span class="k">def</span> <span class="nf">use_required_attribute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">initial</span><span class="p">):</span>
        <span class="c1"># Don&#39;t use the &#39;required&#39; attribute because browser validation would</span>
        <span class="c1"># require all checkboxes to be checked instead of at least one.</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">value_omitted_from_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="c1"># HTML checkboxes don&#39;t appear in POST data if not checked, so it&#39;s</span>
        <span class="c1"># never known if the value is actually omitted.</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">id_for_label</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">id_</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;&quot;</span>
<span class="sd">        Don&#39;t include for=&quot;field_0&quot; in &lt;label&gt; because clicking such a label</span>
<span class="sd">        would toggle the first checkbox.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">index</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="s1">&#39;&#39;</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">CheckboxSelectMultiple</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">id_for_label</span><span class="p">(</span><span class="n">id_</span><span class="p">,</span> <span class="n">index</span><span class="p">)</span></div>


<div class="viewcode-block" id="MultiWidget"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.MultiWidget">[docs]</a><span class="k">class</span> <span class="nc">MultiWidget</span><span class="p">(</span><span class="n">Widget</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A widget that is composed of multiple widgets.</span>

<span class="sd">    In addition to the values added by Widget.get_context(), this widget</span>
<span class="sd">    adds a list of subwidgets to the context as widget[&#39;subwidgets&#39;].</span>
<span class="sd">    These can be looped over and rendered like normal widgets.</span>

<span class="sd">    You&#39;ll probably want to use this class with MultiValueField.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/multiwidget.html&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">widgets</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">widgets</span> <span class="o">=</span> <span class="p">[</span><span class="n">w</span><span class="p">()</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">w</span><span class="p">,</span> <span class="nb">type</span><span class="p">)</span> <span class="k">else</span> <span class="n">w</span> <span class="k">for</span> <span class="n">w</span> <span class="ow">in</span> <span class="n">widgets</span><span class="p">]</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MultiWidget</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">attrs</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">is_hidden</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">all</span><span class="p">(</span><span class="n">w</span><span class="o">.</span><span class="n">is_hidden</span> <span class="k">for</span> <span class="n">w</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">widgets</span><span class="p">)</span>

<div class="viewcode-block" id="MultiWidget.get_context"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.MultiWidget.get_context">[docs]</a>    <span class="k">def</span> <span class="nf">get_context</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span>
        <span class="n">context</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">MultiWidget</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_localized</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">widget</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">widgets</span><span class="p">:</span>
                <span class="n">widget</span><span class="o">.</span><span class="n">is_localized</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_localized</span>
        <span class="c1"># value is a list of values, each corresponding to a widget</span>
        <span class="c1"># in self.widgets.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
            <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">decompress</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

        <span class="n">final_attrs</span> <span class="o">=</span> <span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">][</span><span class="s1">&#39;attrs&#39;</span><span class="p">]</span>
        <span class="n">input_type</span> <span class="o">=</span> <span class="n">final_attrs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;type&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="n">id_</span> <span class="o">=</span> <span class="n">final_attrs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">)</span>
        <span class="n">subwidgets</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">widget</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">widgets</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">input_type</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">widget</span><span class="o">.</span><span class="n">input_type</span> <span class="o">=</span> <span class="n">input_type</span>
            <span class="n">widget_name</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">widget_value</span> <span class="o">=</span> <span class="n">value</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
            <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
                <span class="n">widget_value</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">if</span> <span class="n">id_</span><span class="p">:</span>
                <span class="n">widget_attrs</span> <span class="o">=</span> <span class="n">final_attrs</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
                <span class="n">widget_attrs</span><span class="p">[</span><span class="s1">&#39;id&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">id_</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">widget_attrs</span> <span class="o">=</span> <span class="n">final_attrs</span>
            <span class="n">subwidgets</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">widget</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="n">widget_name</span><span class="p">,</span> <span class="n">widget_value</span><span class="p">,</span> <span class="n">widget_attrs</span><span class="p">)[</span><span class="s1">&#39;widget&#39;</span><span class="p">])</span>
        <span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">][</span><span class="s1">&#39;subwidgets&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">subwidgets</span>
        <span class="k">return</span> <span class="n">context</span></div>

    <span class="k">def</span> <span class="nf">id_for_label</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">id_</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">id_</span><span class="p">:</span>
            <span class="n">id_</span> <span class="o">+=</span> <span class="s1">&#39;_0&#39;</span>
        <span class="k">return</span> <span class="n">id_</span>

    <span class="k">def</span> <span class="nf">value_from_datadict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">widget</span><span class="o">.</span><span class="n">value_from_datadict</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span> <span class="o">+</span> <span class="s1">&#39;_</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">widget</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">widgets</span><span class="p">)]</span>

    <span class="k">def</span> <span class="nf">value_omitted_from_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">all</span><span class="p">(</span>
            <span class="n">widget</span><span class="o">.</span><span class="n">value_omitted_from_data</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span> <span class="o">+</span> <span class="s1">&#39;_</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">i</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">widget</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">widgets</span><span class="p">)</span>
        <span class="p">)</span>

<div class="viewcode-block" id="MultiWidget.decompress"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.MultiWidget.decompress">[docs]</a>    <span class="k">def</span> <span class="nf">decompress</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a list of decompressed values for the given compressed value.</span>
<span class="sd">        The given value can be assumed to be valid, but not necessarily</span>
<span class="sd">        non-empty.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s1">&#39;Subclasses must implement this method.&#39;</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_get_media</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s2">&quot;Media for a multiwidget is the combination of all media of the subwidgets&quot;</span>
        <span class="n">media</span> <span class="o">=</span> <span class="n">Media</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">w</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">widgets</span><span class="p">:</span>
            <span class="n">media</span> <span class="o">=</span> <span class="n">media</span> <span class="o">+</span> <span class="n">w</span><span class="o">.</span><span class="n">media</span>
        <span class="k">return</span> <span class="n">media</span>
    <span class="n">media</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">_get_media</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__deepcopy__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">memo</span><span class="p">):</span>
        <span class="n">obj</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">MultiWidget</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__deepcopy__</span><span class="p">(</span><span class="n">memo</span><span class="p">)</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">widgets</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">widgets</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">obj</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">needs_multipart_form</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">any</span><span class="p">(</span><span class="n">w</span><span class="o">.</span><span class="n">needs_multipart_form</span> <span class="k">for</span> <span class="n">w</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">widgets</span><span class="p">)</span></div>


<div class="viewcode-block" id="SplitDateTimeWidget"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.SplitDateTimeWidget">[docs]</a><span class="k">class</span> <span class="nc">SplitDateTimeWidget</span><span class="p">(</span><span class="n">MultiWidget</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A Widget that splits datetime input into two &lt;input type=&quot;text&quot;&gt; boxes.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">supports_microseconds</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/splitdatetime.html&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">date_format</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">time_format</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">widgets</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">DateInput</span><span class="p">(</span><span class="n">attrs</span><span class="o">=</span><span class="n">attrs</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="n">date_format</span><span class="p">),</span>
            <span class="n">TimeInput</span><span class="p">(</span><span class="n">attrs</span><span class="o">=</span><span class="n">attrs</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="n">time_format</span><span class="p">),</span>
        <span class="p">)</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">SplitDateTimeWidget</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">widgets</span><span class="p">,</span> <span class="n">attrs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">decompress</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">value</span><span class="p">:</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">to_current_timezone</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
            <span class="k">return</span> <span class="p">[</span><span class="n">value</span><span class="o">.</span><span class="n">date</span><span class="p">(),</span> <span class="n">value</span><span class="o">.</span><span class="n">time</span><span class="p">()</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">microsecond</span><span class="o">=</span><span class="mi">0</span><span class="p">)]</span>
        <span class="k">return</span> <span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">]</span></div>


<div class="viewcode-block" id="SplitHiddenDateTimeWidget"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.SplitHiddenDateTimeWidget">[docs]</a><span class="k">class</span> <span class="nc">SplitHiddenDateTimeWidget</span><span class="p">(</span><span class="n">SplitDateTimeWidget</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A Widget that splits datetime input into two &lt;input type=&quot;hidden&quot;&gt; inputs.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/splithiddendatetime.html&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">date_format</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">time_format</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">SplitHiddenDateTimeWidget</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">attrs</span><span class="p">,</span> <span class="n">date_format</span><span class="p">,</span> <span class="n">time_format</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">widget</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">widgets</span><span class="p">:</span>
            <span class="n">widget</span><span class="o">.</span><span class="n">input_type</span> <span class="o">=</span> <span class="s1">&#39;hidden&#39;</span></div>


<div class="viewcode-block" id="SelectDateWidget"><a class="viewcode-back" href="../../../ref/forms/widgets.html#django.forms.SelectDateWidget">[docs]</a><span class="k">class</span> <span class="nc">SelectDateWidget</span><span class="p">(</span><span class="n">Widget</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A Widget that splits date input into three &lt;select&gt; boxes.</span>

<span class="sd">    This also serves as an example of a Widget that has more than one HTML</span>
<span class="sd">    element and hence implements value_from_datadict.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">none_value</span> <span class="o">=</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="s1">&#39;---&#39;</span><span class="p">)</span>
    <span class="n">month_field</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_month&#39;</span>
    <span class="n">day_field</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_day&#39;</span>
    <span class="n">year_field</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_year&#39;</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s1">&#39;django/forms/widgets/select_date.html&#39;</span>
    <span class="n">input_type</span> <span class="o">=</span> <span class="s1">&#39;select&#39;</span>
    <span class="n">select_widget</span> <span class="o">=</span> <span class="n">Select</span>
    <span class="n">date_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;(\d</span><span class="si">{4}</span><span class="s1">)-(\d\d?)-(\d\d?)$&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attrs</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">years</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">months</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">empty_label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">attrs</span> <span class="o">=</span> <span class="n">attrs</span> <span class="ow">or</span> <span class="p">{}</span>

        <span class="c1"># Optional list or tuple of years to use in the &quot;year&quot; select box.</span>
        <span class="k">if</span> <span class="n">years</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">years</span> <span class="o">=</span> <span class="n">years</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">this_year</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()</span><span class="o">.</span><span class="n">year</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">years</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="n">this_year</span><span class="p">,</span> <span class="n">this_year</span> <span class="o">+</span> <span class="mi">10</span><span class="p">)</span>

        <span class="c1"># Optional dict of months to use in the &quot;month&quot; select box.</span>
        <span class="k">if</span> <span class="n">months</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">months</span> <span class="o">=</span> <span class="n">months</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">months</span> <span class="o">=</span> <span class="n">MONTHS</span>

        <span class="c1"># Optional string, list, or tuple to use as empty_label.</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">empty_label</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">len</span><span class="p">(</span><span class="n">empty_label</span><span class="p">)</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;empty_label list/tuple must have 3 elements.&#39;</span><span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">year_none_value</span> <span class="o">=</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">empty_label</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">month_none_value</span> <span class="o">=</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">empty_label</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">day_none_value</span> <span class="o">=</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">empty_label</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">empty_label</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">none_value</span> <span class="o">=</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">empty_label</span><span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">year_none_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">none_value</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">month_none_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">none_value</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">day_none_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">none_value</span>

    <span class="k">def</span> <span class="nf">get_context</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span>
        <span class="n">context</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">SelectDateWidget</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">attrs</span><span class="p">)</span>
        <span class="n">date_context</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">year_choices</span> <span class="o">=</span> <span class="p">[(</span><span class="n">i</span><span class="p">,</span> <span class="n">force_text</span><span class="p">(</span><span class="n">i</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">years</span><span class="p">]</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_required</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
            <span class="n">year_choices</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">year_none_value</span><span class="p">)</span>
        <span class="n">year_attrs</span> <span class="o">=</span> <span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">][</span><span class="s1">&#39;attrs&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="n">year_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">year_field</span> <span class="o">%</span> <span class="n">name</span>
        <span class="n">year_attrs</span><span class="p">[</span><span class="s1">&#39;id&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;id_</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">year_name</span>
        <span class="n">date_context</span><span class="p">[</span><span class="s1">&#39;year&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">select_widget</span><span class="p">(</span><span class="n">attrs</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="n">year_choices</span><span class="p">)</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span>
            <span class="n">name</span><span class="o">=</span><span class="n">year_name</span><span class="p">,</span>
            <span class="n">value</span><span class="o">=</span><span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">][</span><span class="s1">&#39;value&#39;</span><span class="p">][</span><span class="s1">&#39;year&#39;</span><span class="p">],</span>
            <span class="n">attrs</span><span class="o">=</span><span class="n">year_attrs</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">month_choices</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">months</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_required</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
            <span class="n">month_choices</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">month_none_value</span><span class="p">)</span>
        <span class="n">month_attrs</span> <span class="o">=</span> <span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">][</span><span class="s1">&#39;attrs&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="n">month_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">month_field</span> <span class="o">%</span> <span class="n">name</span>
        <span class="n">month_attrs</span><span class="p">[</span><span class="s1">&#39;id&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;id_</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">month_name</span>
        <span class="n">date_context</span><span class="p">[</span><span class="s1">&#39;month&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">select_widget</span><span class="p">(</span><span class="n">attrs</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="n">month_choices</span><span class="p">)</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span>
            <span class="n">name</span><span class="o">=</span><span class="n">month_name</span><span class="p">,</span>
            <span class="n">value</span><span class="o">=</span><span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">][</span><span class="s1">&#39;value&#39;</span><span class="p">][</span><span class="s1">&#39;month&#39;</span><span class="p">],</span>
            <span class="n">attrs</span><span class="o">=</span><span class="n">month_attrs</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">day_choices</span> <span class="o">=</span> <span class="p">[(</span><span class="n">i</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">32</span><span class="p">)]</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_required</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
            <span class="n">day_choices</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">day_none_value</span><span class="p">)</span>
        <span class="n">day_attrs</span> <span class="o">=</span> <span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">][</span><span class="s1">&#39;attrs&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="n">day_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">day_field</span> <span class="o">%</span> <span class="n">name</span>
        <span class="n">day_attrs</span><span class="p">[</span><span class="s1">&#39;id&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;id_</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">day_name</span>
        <span class="n">date_context</span><span class="p">[</span><span class="s1">&#39;day&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">select_widget</span><span class="p">(</span><span class="n">attrs</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="n">day_choices</span><span class="p">,)</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span>
            <span class="n">name</span><span class="o">=</span><span class="n">day_name</span><span class="p">,</span>
            <span class="n">value</span><span class="o">=</span><span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">][</span><span class="s1">&#39;value&#39;</span><span class="p">][</span><span class="s1">&#39;day&#39;</span><span class="p">],</span>
            <span class="n">attrs</span><span class="o">=</span><span class="n">day_attrs</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">subwidgets</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parse_date_fmt</span><span class="p">():</span>
            <span class="n">subwidgets</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">date_context</span><span class="p">[</span><span class="n">field</span><span class="p">][</span><span class="s1">&#39;widget&#39;</span><span class="p">])</span>
        <span class="n">context</span><span class="p">[</span><span class="s1">&#39;widget&#39;</span><span class="p">][</span><span class="s1">&#39;subwidgets&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">subwidgets</span>
        <span class="k">return</span> <span class="n">context</span>

    <span class="k">def</span> <span class="nf">format_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a dict containing the year, month, and day of the current value.</span>
<span class="sd">        Use dict instead of a datetime to allow invalid dates such as February</span>
<span class="sd">        31 to display correctly.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">year</span><span class="p">,</span> <span class="n">month</span><span class="p">,</span> <span class="n">day</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">)):</span>
            <span class="n">year</span><span class="p">,</span> <span class="n">month</span><span class="p">,</span> <span class="n">day</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="n">value</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">value</span><span class="o">.</span><span class="n">day</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">settings</span><span class="o">.</span><span class="n">USE_L10N</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">input_format</span> <span class="o">=</span> <span class="n">get_format</span><span class="p">(</span><span class="s1">&#39;DATE_INPUT_FORMATS&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
                    <span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="n">force_str</span><span class="p">(</span><span class="n">value</span><span class="p">),</span> <span class="n">input_format</span><span class="p">)</span>
                    <span class="n">year</span><span class="p">,</span> <span class="n">month</span><span class="p">,</span> <span class="n">day</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="n">d</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">d</span><span class="o">.</span><span class="n">day</span>
                <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                    <span class="k">pass</span>
            <span class="n">match</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">date_re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">match</span><span class="p">:</span>
                <span class="n">year</span><span class="p">,</span> <span class="n">month</span><span class="p">,</span> <span class="n">day</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">match</span><span class="o">.</span><span class="n">groups</span><span class="p">()]</span>
        <span class="k">return</span> <span class="p">{</span><span class="s1">&#39;year&#39;</span><span class="p">:</span> <span class="n">year</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">:</span> <span class="n">month</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">:</span> <span class="n">day</span><span class="p">}</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_parse_date_fmt</span><span class="p">():</span>
        <span class="n">fmt</span> <span class="o">=</span> <span class="n">get_format</span><span class="p">(</span><span class="s1">&#39;DATE_FORMAT&#39;</span><span class="p">)</span>
        <span class="n">escaped</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">for</span> <span class="n">char</span> <span class="ow">in</span> <span class="n">fmt</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">escaped</span><span class="p">:</span>
                <span class="n">escaped</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="k">elif</span> <span class="n">char</span> <span class="o">==</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">:</span>
                <span class="n">escaped</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">elif</span> <span class="n">char</span> <span class="ow">in</span> <span class="s1">&#39;Yy&#39;</span><span class="p">:</span>
                <span class="k">yield</span> <span class="s1">&#39;year&#39;</span>
            <span class="k">elif</span> <span class="n">char</span> <span class="ow">in</span> <span class="s1">&#39;bEFMmNn&#39;</span><span class="p">:</span>
                <span class="k">yield</span> <span class="s1">&#39;month&#39;</span>
            <span class="k">elif</span> <span class="n">char</span> <span class="ow">in</span> <span class="s1">&#39;dj&#39;</span><span class="p">:</span>
                <span class="k">yield</span> <span class="s1">&#39;day&#39;</span>

    <span class="k">def</span> <span class="nf">id_for_label</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">id_</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">first_select</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parse_date_fmt</span><span class="p">():</span>
            <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">id_</span><span class="p">,</span> <span class="n">first_select</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_month&#39;</span> <span class="o">%</span> <span class="n">id_</span>

    <span class="k">def</span> <span class="nf">value_from_datadict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="n">y</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">year_field</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>
        <span class="n">m</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">month_field</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>
        <span class="n">d</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">day_field</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">y</span> <span class="o">==</span> <span class="n">m</span> <span class="o">==</span> <span class="n">d</span> <span class="o">==</span> <span class="s2">&quot;0&quot;</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">y</span> <span class="ow">and</span> <span class="n">m</span> <span class="ow">and</span> <span class="n">d</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">settings</span><span class="o">.</span><span class="n">USE_L10N</span><span class="p">:</span>
                <span class="n">input_format</span> <span class="o">=</span> <span class="n">get_format</span><span class="p">(</span><span class="s1">&#39;DATE_INPUT_FORMATS&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">date_value</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">y</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">m</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">d</span><span class="p">))</span>
                <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                    <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">-</span><span class="si">%s</span><span class="s1">-</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">d</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">date_value</span> <span class="o">=</span> <span class="n">datetime_safe</span><span class="o">.</span><span class="n">new_date</span><span class="p">(</span><span class="n">date_value</span><span class="p">)</span>
                    <span class="k">return</span> <span class="n">date_value</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="n">input_format</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">-</span><span class="si">%s</span><span class="s1">-</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">d</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">value_omitted_from_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">files</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="ow">not</span> <span class="nb">any</span><span class="p">(</span>
            <span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">_</span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">interval</span><span class="p">)</span> <span class="ow">in</span> <span class="n">data</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">interval</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">)</span>
        <span class="p">)</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>